package com.weakteam.orderonline.adminconsole.gwt.client.widget;

import com.allen_sauer.gwt.dnd.client.DragContext;
import com.allen_sauer.gwt.dnd.client.drop.FlowPanelDropController;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;


/**
 * @author Nail
 */
public class MenuCategoryDropController extends FlowPanelDropController {

    private int dragIndex;

    public MenuCategoryDropController(MenuCategoryContainer dropTarget) {
        super(dropTarget);
    }

    @Override
    public void onDrop(DragContext context) {
        super.onDrop(context);
        // fire order-change event if widget changed index
        Widget widget = context.selectedWidgets.get(0);
        int dropIndex = this.dropTarget.getWidgetIndex(widget);
        if (dropIndex != dragIndex) {
            ((MenuCategoryContainer) this.dropTarget).orderChangeEventFire();
        }
    }

    @Override
    public void onEnter(DragContext context) {
        // save selected widget index before super handler
        Widget widget = context.selectedWidgets.get(0);
        int ii = this.dropTarget.getWidgetIndex(widget);
        if (ii >= 0) { dragIndex = ii; }
        // call super onEnter
        super.onEnter(context);
        // set item selected if it's not already active
        MenuCategoryBox newSelectedBox = (MenuCategoryBox) widget;
        if (!newSelectedBox.isSelected()) {
            ((MenuCategoryContainer) this.dropTarget).setBoxSelected(newSelectedBox);
        }
    }

    @Override
    protected Widget newPositioner(DragContext context) {
        HTML positioner = new HTML();
        positioner.setStyleName("category-item category-item-positioner");
        return positioner;
    }

}
